package com.todod.service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.entity.QueryEntry;
import com.todod.mapper.BaseNoticeMapper;
import com.todod.model.BaseNotice;
import com.todod.model.ProjectInfo;

import net.logstash.logback.encoder.org.apache.commons.lang.StringUtils;

@Service
public class BaseNoticeSercice extends ServiceImpl<BaseNoticeMapper, BaseNotice> {
	@Autowired
	private BaseNoticeMapper baseNoticeMapper;
	@Autowired
	public ProjectInfoService projectInfoService;

	/**
	 * 
	 * @param queryEntry
	 * @param departmentId
	 * @return
	 */
	public IPage<BaseNotice> getList(QueryEntry queryEntry) {
		Page<BaseNotice> page = new Page<BaseNotice>(queryEntry.getPage(), queryEntry.getLimit());
		QueryWrapper<BaseNotice> wrapper = new QueryWrapper<BaseNotice>();

		if (queryEntry.getTitle() != null && StringUtils.isNotBlank(queryEntry.getTitle()))
			wrapper.like("bg_title_k1", queryEntry.getTitle());

		if (queryEntry.getType() != null && StringUtils.isNotBlank(queryEntry.getType()))
			wrapper.eq("bg_type_k1", queryEntry.getType());

		if (queryEntry.getXmId() != null)
			wrapper.eq("bg_xm_id", queryEntry.getXmId());

		if (queryEntry.getBeginDate() != null && queryEntry.getEndDate() == null)
			wrapper.ge("bg_fabu_da", queryEntry.getBeginDate());
		if (queryEntry.getEndDate() != null && queryEntry.getBeginDate() == null)
			wrapper.le("bg_fabu_da", queryEntry.getEndDate());
		if (queryEntry.getBeginDate() != null && queryEntry.getEndDate() != null)
			wrapper.between("bg_fabu_da", queryEntry.getBeginDate(), queryEntry.getEndDate());
		wrapper.orderByDesc("bg_fabu_da");
		IPage<BaseNotice> iPage = baseNoticeMapper.selectPage(page, wrapper);

		List<BaseNotice> records = iPage.getRecords();
		for (BaseNotice record : records) {
			ProjectInfo projectInfo = projectInfoService.getXMInfoByXmId(record.getXmId());

			record.setXmName(projectInfo.getXiangmumingcheng());
		}
		return iPage;
	}

	/*
	 * 新增
	 */
	/**
	 * 
	 * @param _userId
	 * @param baseNotice
	 * @param departmentId
	 */
	public void saveData(Long _userId, BaseNotice baseNotice) {
		baseNotice.setId(SnowflakeIdGenerator.getId());

		baseNotice.setStatus("1");
		baseNotice.setCreateUserId(_userId);
		baseNotice.setCreateTime(LocalDateTime.now());
		baseNoticeMapper.insert(baseNotice);
	}

	/*
	 * 编辑
	 */
	/**
	 * 
	 * @param _userId
	 * @param baseNotice
	 */
	public void editData(Long _userId, BaseNotice baseNotice) {
		baseNotice.setModifyTime(LocalDateTime.now());
		baseNotice.setCreateUserId(_userId);
		baseNoticeMapper.updateById(baseNotice);
	}

	/*
	 * 数据回显
	 */
	public Map<String, Object> getInfo(Long id) {
		Map<String, Object> map = new HashMap<>();
		BaseNotice baseNotice = baseNoticeMapper.selectById(id);
		map.put("notcie", baseNotice);
		return map;
	}

	public void deleteData(String ids) {
		String[] id = ids.split(",");
		for (int i = 0; i < id.length; i++) {
			baseNoticeMapper.deleteById(Long.valueOf(id[i]));
		}
	}

	public List<BaseNotice> getNoticeList(String xmId, String type) {
		QueryWrapper<BaseNotice> wrapper = new QueryWrapper<BaseNotice>();
		if (type != null && StringUtils.isNotBlank(type)) {
			wrapper.eq("bg_type_k1", type);
		}

		wrapper.eq("bg_xm_id", xmId);
		wrapper.orderByDesc("bg_fabu_da");

		List<BaseNotice> list = baseNoticeMapper.selectList(wrapper);
		return list;
	}

}
